/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.customer;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.customer.Country;
import src.domain.customer.Province;
import src.services.customer.CountryModel;
import src.services.customer.ProvinceModel;
import src.util.Messager;

/**
 *
 * @author jay
 */
@ManagedBean
@SessionScoped
public class ProvinceBean implements Serializable {

    private List<Province> provinces;
    private List<SelectItem> avaibleCountry;
    private Province selectedData, insertedData;
    private CountryModel coutryHelper;
    private boolean editMode = true;
    private String filter;

    public ProvinceBean() {
        ProvinceModel helper = new ProvinceModel();
        
        this.provinces = helper.findAll(null);
        this.insertedData = null;
        this.selectedData = null;
        initForm();
    }

    public boolean isEditMode() {
        return editMode;
    }
    
    public List<Province> getProvinces() {
        return this.provinces;
    }
    
    public Province getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(Province selectedData) {
        this.selectedData = selectedData;
    }
    
    public Province getInsertedData() {
        return insertedData;
    }

    public void setInsertedData(Province insertedData) {
        this.insertedData = insertedData;
    }
    
    public List<SelectItem> getAvaibleCountry() {
        return avaibleCountry;
    }

    public void setAvaibleCountry(List<SelectItem> avaibleCountry) {
        this.avaibleCountry = avaibleCountry;
    }
    
    public String getFilter() {
        return filter;
    }

    public void setFilter(String filter) {
        this.filter = filter;
    }
    
    public void search() {
        Map<String, String> filters = new HashMap<>();
      
        filters.put("provinceName", getFilter());
        ProvinceModel helper = new ProvinceModel();      
        this.provinces = helper.findAll(filters);
        setFilter("");
    }
    
    public void handleClose(CloseEvent event) {
        this.selectedData = null;
        this.insertedData = null;
        this.editMode = true;      
        coutryHelper = null;
    }
    
    public void initForm() {
        coutryHelper = new CountryModel();
        avaibleCountry = coutryHelper.listAll();
    }
    
    public void openData() { 
        initForm();
        this.editMode = true;
        this.insertedData = Province.clone(selectedData);
    }
    
    public void newData() { 
        initForm();
        this.editMode = false;
        this.selectedData = null;
        this.insertedData = new Province();
                
    }
    
    public void save() {
        
        RequestContext context = RequestContext.getCurrentInstance();  
        ProvinceModel helper = new ProvinceModel();
        boolean success = helper.saveData(insertedData);
        if (success==true) {
            if (this.editMode==false) this.provinces.add(insertedData);
            else {
                int i = provinces.indexOf(selectedData);
                if (i <= provinces.size()) provinces.set(i, Province.clone(insertedData));
            }
            editMode = true;
            
        } else {
            if (this.editMode==false) insertedData.setProvinceId(null);
        } 
        
        /*boolean success;
             
        Province data = this.insertedData;
        if (this.insertedData == null) data = this.selectedData;
        ProvinceModel helper = new ProvinceModel();
        success = helper.saveData(data);
        if (success && this.editMode!=true) {
            this.provinces.add(this.insertedData);
        }
        
        * 
        */
        if(!success) Messager.showSaveUpdateStatus(success);
        context.addCallbackParam("success", success);  
    }
    
    public void delete() {
        
        RequestContext context = RequestContext.getCurrentInstance();  
        boolean success;  
        ProvinceModel helper = new ProvinceModel();
        success = helper.deleteData(this.selectedData);
        
        if (success) {
            this.provinces.remove(this.selectedData);
            this.selectedData = null;
        }
               
        if(!success) Messager.showDeleteStatusConstaint(success);
        context.addCallbackParam("success", success);  
    }
    
    public String findCountryName(Integer id) {
        String nama = "";
        if (id!=null && !id.equals(0)) {
            int idx=0;
            boolean found=false;
            while (idx<avaibleCountry.size() && !found) {
                SelectItem it = avaibleCountry.get(idx);
                found = id.equals(it.getValue()) ? true:false;
                if (found) nama = it.getLabel();
                idx++;
            }
        }
        return nama;
    }
}
